$(document).ready(function(){

	//listflex
	$('.listFlex').flexigrid
	(
		{
			autoload: true,
			height: $('#main').height(),
			title: 'Team List',
			url: root + module + '/listview',
			blockOpacity: 0,
			dataType: 'json',
			colModel : [
				{display: '', name : 'a.group_id', width : 40, align:'center', process: viewItem}
				,{display: 'Group Name', name : 'a.group_name', width : 170, align : 'left', sortable:true}
				,{display: 'Parent Team', name : 'parent_name', width : 170, align : 'left', sortable:true}
				,{display: 'Members', name : 'members_count', width : 100, align : 'left', sortable:true}
				,{display: 'Approvers', name : 'approvers_count', width : 100, align : 'left', sortable:true}
				// ,{display: 'Username', name : 'a.username', width : 100, align : 'left', sortable:true}
				// ,{display: 'Email', name : 'a.email', width : 150, align : 'left', sortable:true}
				// ,{display: 'Role', name : 'b.role_name', width : 80, align : 'left', sortable:true}
				],
			buttons : [
				{name: 'Add', bclass: 'add', onpress : addItem},
				//{separator: true},
				{name: 'Delete', bclass: 'delete', onpress : deleteItem},
				//{separator: true}
			],
			newp: 1,
			sortname: "a.group_name",
			sortorder: "asc",
			rp: 40,
			usepager: true,
			useRp: true,
			resizable: true,
			showTableToggleBtn: true,
			singleSelect: false
		}
	);

	//list of employees in a team
	$('.membersFlex').flexigrid
	(
		{
			autoload: true,
			height: 300,
			title: 'Team Members',
			url: root + module + '/listViewTeamEmployee/' + $('input[name=item]').val(),
			blockOpacity: 0,
			dataType: 'json',
			colModel : [
				{display: '', name : 'a.employee_group_id', width : 40, align:'center', process: viewItem, hide:true}
				,{display: 'Employee No.', name : 'b.employee_no', width : 66, align : 'left', sortable:true}
				,{display: 'Employee Name', name : 'b.fullname', width : 160, align : 'left', sortable:true}
				,{display: 'Username', name : 'b.username', width : 100, align : 'left', sortable:true}
				,{display: 'Email', name : 'b.email', width : 150, align : 'left', sortable:true}
				,{display: 'Is Manager', name : 'a.is_manager', width : 80, align : 'left', sortable:true, process: isManager}
				],
			buttons : [
				{name: 'Add Members', bclass: 'add', onpress : addTeamEmployee},
				//{separator: true},
				{name: 'Delete Members', bclass: 'delete', onpress : deleteTeamEmployee},
				//{separator: true}
			],
			newp: 1,
			// sortname: "a.is_manager, b.fullname",
			// sortorder: "asc",
			rp: 40,
			usepager: true,
			useRp: true,
			resizable: true,
			showTableToggleBtn: true,
			singleSelect: false
		}
	);
	
	//list of employees in a team
	$('.approversFlex').flexigrid
	(
		{
			autoload: true,
			height: 150,
			title: 'Team Members',
			url: root + module + '/listViewTeamApprover/' + $('input[name=item]').val(),
			blockOpacity: 0,
			dataType: 'json',
			colModel : [
				{display: '', name : 'a.employee_group_id', width : 40, align:'center', process: viewItem, hide:true}
				,{display: 'Employee No.', name : 'b.employee_no', width : 66, align : 'left', sortable:true}
				,{display: 'Employee Name', name : 'b.fullname', width : 160, align : 'left', sortable:true}
				,{display: 'Username', name : 'b.username', width : 100, align : 'left', sortable:true}
				,{display: 'Email', name : 'b.email', width : 150, align : 'left', sortable:true}
				],
			buttons : [
				{name: 'Add Approvers', bclass: 'add', onpress : addTeamApprover},
				//{separator: true},
				{name: 'Delete Approvers', bclass: 'delete', onpress : deleteTeamApprover},
				//{separator: true}
			],
			newp: 1,
			// sortname: "a.is_manager, b.fullname",
			// sortorder: "asc",
			rp: 40,
			usepager: true,
			useRp: true,
			resizable: true,
			showTableToggleBtn: true,
			singleSelect: false
		}
	);


	function addTeamEmployee()
	{
		//set flexigrid		
		$('.dialogFlex').flexigrid
		(
			{
				autoload: true,
				title: 'Team Members',
				url: root + 'employees/listview',
				blockOpacity: 0,
				dataType: 'json',
				colModel : [
					{display: '', name : 'a.id', width : 40, align:'center', hide: true}
					,{display: 'Employee No.', name : 'a.employee_no', width : 66, align : 'left', sortable:true}
					,{display: 'Employee Name', name : 'a.fullname', width : 160, align : 'left', sortable:true}
					,{display: 'Username', name : 'a.username', width : 100, align : 'left', sortable:true}
					,{display: 'Email', name : 'a.email', width : 150, align : 'left', sortable:true}
					//,{display: 'Role', name : 'b.role_name', width : 80, align : 'left', sortable:true}
					],
				newp: 1,
				sortname: "a.fullname",
				sortorder: "asc",
				rp: 40,
				usepager: true,
				useRp: true,
				resizable: true,
				showTableToggleBtn: true,
				singleSelect: false
			}
		);
		showDialog();
		//add employees to team from dialog
		$('#dialog-add').unbind('click').click(function(){
			addTeamEmployeeClick();
		});
	}
	
	function addTeamApprover()
	{
		//set flexigrid		
		$('.dialogFlex').flexigrid
		(
			{
				autoload: true,
				title: 'Team Members',
				url: root + 'employees/listview',
				blockOpacity: 0,
				dataType: 'json',
				colModel : [
					{display: '', name : 'a.id', width : 40, align:'center', hide: true}
					,{display: 'Employee No.', name : 'a.employee_no', width : 66, align : 'left', sortable:true}
					,{display: 'Employee Name', name : 'a.fullname', width : 160, align : 'left', sortable:true}
					,{display: 'Username', name : 'a.username', width : 100, align : 'left', sortable:true}
					,{display: 'Email', name : 'a.email', width : 150, align : 'left', sortable:true}
					//,{display: 'Role', name : 'b.role_name', width : 80, align : 'left', sortable:true}
					],
				newp: 1,
				sortname: "a.fullname",
				sortorder: "asc",
				rp: 40,
				usepager: true,
				useRp: true,
				resizable: true,
				showTableToggleBtn: true,
				singleSelect: false
			}
		);
		showDialog();
		//add approver to team from dialog
		$('#dialog-add').unbind('click').click(function(){
			addTeamApproverClick();
		});
	}

	function showDialog()
	{
		var dt = [{name:'squery',value:''}];
		$('.dialogFlex').flexOptions({params:dt}).flexReload();
		$('.dialog #keyword').val('');
		
		$('.dialog').dialog({
			draggable: false,
			modal: true,
			resizable: false,
			width:700,
			title: 'Employee List'
		});
	}

	function deleteTeamEmployee()
	{
		if($('.membersFlex .trSelected').length)
		{
			if(confirm('Are you sure you want to delete selected item/s?'))
			{
				showLoader('#main');
				var data = new Array();
				var count = 0;
				$('.membersFlex .trSelected .fview').each(function(){
					data[count] = $(this).attr('id');
					count++;
				});

				var action = root + module + '/deleteteamemployee';
				var dt = {items: data};

				$.ajax({
				   type: 'POST',
				   url: action,
				   data: dt,
				   dataType: 'json',
				   success: function(data){
						hideLoader();
						$('.membersFlex').flexReload();
					},
				   error: function () { onError(); hideLoader(); }
				 });
			}
		}
		else
		{
			alert("Please select item/s to delete.");
		}
	}
	
	function deleteTeamApprover()
	{
		if($('.approversFlex .trSelected').length)
		{
			if(confirm('Are you sure you want to delete selected item/s?'))
			{
				showLoader('#main');
				var data = new Array();
				var count = 0;
				$('.approversFlex .trSelected .fview').each(function(){
					data[count] = $(this).attr('id');
					count++;
				});

				var action = root + module + '/deleteteamapprover';
				var dt = {items: data};

				$.ajax({
				   type: 'POST',
				   url: action,
				   data: dt,
				   dataType: 'json',
				   success: function(data){
						hideLoader();
						$('.approversFlex').flexReload();
					},
				   error: function () { onError(); hideLoader(); }
				 });
			}
		}
		else
		{
			alert("Please select item/s to delete.");
		}
	}	
	
	function addTeamEmployeeClick()
	{
		if($('.dialogFlex .trSelected').length)
		{
			showLoader('#main');
			var data = new Array();
			var count = 0;
			$('.dialogFlex .trSelected .fview').each(function(){
				data[count] = $(this).attr('id');
				count++;
			});			
			var action = root + module + '/addTeamEmployee';
			var dt = {items: data, group_id: $('input[name=item]').val()};
			$('.dialog').dialog('close');
			$.ajax({
			   type: 'POST',
			   url: action,
			   data: dt,
			   dataType: 'json',
			   success: function(data){
					hideLoader();
					$('.membersFlex').flexReload();
				},
			   error: function () { onError(); hideLoader(); }
			 });
		}
		else
		{
			alert("Please select employees to add.");
		}
	}
	
	function addTeamApproverClick()
	{
		if($('.dialogFlex .trSelected').length)
		{
			showLoader('#main');
			var data = new Array();
			var count = 0;
			$('.dialogFlex .trSelected .fview').each(function(){
				data[count] = $(this).attr('id');
				count++;
			});			
			var action = root + module + '/addTeamApprover';
			var dt = {items: data, group_id: $('input[name=item]').val()};
			$('.dialog').dialog('close');
			$.ajax({
			   type: 'POST',
			   url: action,
			   data: dt,
			   dataType: 'json',
			   success: function(data){
					hideLoader();
					$('.approversFlex').flexReload();
				},
			   error: function () { onError(); hideLoader(); }
			 });
		}
		else
		{
			alert("Please select employees to add.");
		}
	}
	
	//search functionality team employee list
	$('#search-flexigrid-employee').click(function(){
		var dt = [{name:'squery',value:$('#keyword-team-employee').val()}];
		$('.membersFlex').flexOptions({params:dt}).flexReload();
	});
	$('#keyword-team-employee').keyup(function(event){
		if(event.keyCode==13)
			$('#search-flexigrid-employee').trigger('click');
	});
	$('#clear-flexigrid-employee').click(function(){
		$('#keyword-team-employee').val('');
		var dt = [{name:'squery',value:$('#keyword-team-employee').val()}];
		$('.membersFlex').flexOptions({params:dt}).flexReload();
	});
	
	//search functionality team approver list
	$('#search-flexigrid-approver').click(function(){
		var dt = [{name:'squery',value:$('#keyword-team-approver').val()}];
		$('.approversFlex').flexOptions({params:dt}).flexReload();
	});
	$('#keyword-team-approver').keyup(function(event){
		if(event.keyCode==13)
			$('#search-flexigrid-approver').trigger('click');
	});
	$('#clear-flexigrid-approver').click(function(){
		$('#keyword-team-approver').val('');
		var dt = [{name:'squery',value:$('#keyword-team-approver').val()}];
		$('.approversFlex').flexOptions({params:dt}).flexReload();
	});
	
	//dropdown is manager
	
	function isManager(celDiv,id)
	{
		var is_manager =celDiv.innerHTML;
		var selected = '';
		if(is_manager == '1')
			selected = 'selected="selected"';
		
		celDiv.innerHTML = '<select id="'+id+'"  title="Is Manager"><option value="0">No</option><option '+selected+' value="1">Yes</option></select>';
		$(celDiv).children('select').change(function(){
			var id = $(this).attr('id');
			var val = $(this).val();
			
			var action = root + module + '/updateTeamEmployee';
			var dt = {employee_group_id: id, is_manager: val};
			showLoader('#main');
			$.ajax({
			   type: 'POST',
			   url: action,
			   data: dt,
			   dataType: 'json',
			   success: function(data){
					hideLoader();
					$('.membersFlex').flexReload();
				},
			   error: function () { onError(); hideLoader(); }
			 });
		});
	} 	
	
});